package com.open.cloud.rbac.controller.orgpost;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.entity.orgpost.CorePost;
import com.open.cloud.rbac.object.orgpost.CorePostDto;
import com.open.cloud.rbac.object.orgpost.CorePostSelectDto;
import com.open.cloud.rbac.service.orgpost.ICorePostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 岗位表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-岗位业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/post")
public class CorePostController {

    @Resource
    private ICorePostService postService;


    @ApiOperation(value = "创建")
    @PostMapping("/add")
    public ResponseWrapper addPostInfo(@RequestBody CorePostDto obj) {
        JSONObject data = postService.addPostInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updatePostInfo(@RequestBody CorePostDto obj) {
        JSONObject data = postService.updatePostInfo(obj);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deletePostById(@RequestParam(name = "id") String id) {
        JSONObject data = postService.deletePostInfo(id);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    ResponseWrapper<IPage<CorePost>> getPostPage(@RequestBody CorePostSelectDto postSelectDto) {
        IPage<CorePost> data = postService.selectPostPage(postSelectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "根据部门Id查询岗位")
    @GetMapping("/listByOrgId")
    public ResponseWrapper getPostListByOrgId(@RequestParam(name = "orgId") String orgId) {
        List<CorePost> data = postService.getPostListByOrgId(orgId);
        return ResponseWrapper.success(data);
    }

}

